<template>
  <div class="s-d-out">
    <div v-if="!ready" class="m-load" v-loading="!ready">
      题解加载中
    </div>
    <div v-else-if="ready && detail == null ">
      <el-empty description="题解信息为空"></el-empty>
    </div>
    <div v-else class="s-d-block">
      <div class="s-d-title">
        {{ detail.title }}
      </div>

      <div class="s-d-info">
        <div class="teacher-icon" v-if="detail.isTeacher">
        <span style="font-size: 20px">🌟</span> 教师回答
      </div>
        <div class="s-d-author">
          作者 🙋： {{detail.nickname}}
        </div>
        <div class="s-d-time">
          创作日期⌚： {{new Date(detail.time * 1000).toLocaleDateString()}}
        </div>
      </div>
      <div class="s-d-content" v-html="detail.content"></div>
    </div>
  </div>

</template>

<script>
import editor from 'mavon-editor'
import {Solution} from "../../utils/http/solutions";

const markdownIt = editor.markdownIt;

export default {
  name: "ArticleBrowser",
  data: function () {
    return {
      ready: false,
      detail: null
    }
  },
  methods:{
    async getDetail(){
      let resp = await Solution.getById(this.$route.params.articleId)
      if (resp.success) {
        this.ready = true
        if (resp.data.id === 0){
          this.$warning("该题解不存在！")
          this.detail = null
          return
        }
        resp.data.content = markdownIt.render(resp.data.content)
        this.detail = resp.data;
      } else {
        this.$error("获取题解列表失败，请稍后重试")
      }
    }
  },
  mounted() {
    this.getDetail()
  },
  watch: {
    $route(to, from) {
      if (to.name === "work-article"){
        this.getDetail()
      }
    }
  }
}
</script>

<style scoped>
.m-load {
  height: 200px;
  padding-top: 100px;
  font-size: 30px;
}
.s-d-out{
  height: 100%;
}
.s-d-block {
  overflow-y: auto;
  max-height: 100%;
  box-sizing: border-box;
  padding: 40px 50px;
  display: flex;
  flex-direction: column;
  position: absolute;
  width: 100%;
}
.s-d-title{
  font-size: 22px;
}
.s-d-info{
  display: flex;
  border: 1px solid #ddd;
  border-radius: 5px;
  box-shadow: 0 2px 6px 0 rgba(0, 0, 0, .12);
  background: white;
  padding: 10px;
  font-size: 16px;
  justify-content: space-around;
  align-items: center;
  margin: 30px 50px;
  position: relative;
}

.s-d-content{
  text-align: left;
}

.teacher-icon{
  position: absolute;
  top: -30px;
  left: -10px;
  background: white;
  z-index: 9;
  color: #53a8ff;
  user-select: none;
  border: 1px solid #53a8ff;
  border-radius: 10px 0 0 0;
  padding: 3px;
  font-size: 16px;
}
</style>
